package cn.iocoder.yudao.module.diy.controller.admin.page;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.diy.controller.admin.page.vo.*;
import cn.iocoder.yudao.module.diy.dal.dataobject.page.PageDO;
import cn.iocoder.yudao.module.diy.service.page.PageService;

@Tag(name = "管理后台 - 装修页面")
@RestController
@RequestMapping("/diy/page")
@Validated
public class PageController {
    @Resource
    private PageService pageService;

    @PostMapping("/create")
    @Operation(summary = "创建装修页面")
    @PreAuthorize("@ss.hasPermission('diy:page:create')")
    public CommonResult<Long> createPage(@Valid @RequestBody PageSaveReqVO createReqVO) {
        return success(pageService.createPage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新装修页面")
    @PreAuthorize("@ss.hasPermission('diy:page:update')")
    public CommonResult<Boolean> updatePage(@Valid @RequestBody PageSaveReqVO updateReqVO) {
        pageService.updatePage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除装修页面")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('diy:page:delete')")
    public CommonResult<Boolean> deletePage(@RequestParam("id") Long id) {
        pageService.deletePage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得装修页面")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('diy:page:query')")
    public CommonResult<PageRespVO> getPage(@RequestParam("id") Long id) {
        PageDO page = pageService.getPage(id);
        return success(BeanUtils.toBean(page, PageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得装修页面分页")
    @PreAuthorize("@ss.hasPermission('diy:page:query')")
    public CommonResult<PageResult<PageRespVO>> getPagePage(@Valid PagePageReqVO pageReqVO) {
        PageResult<PageDO> pageResult = pageService.getPagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出装修页面 Excel")
    @PreAuthorize("@ss.hasPermission('diy:page:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPageExcel(@Valid PagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PageDO> list = pageService.getPagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "装修页面.xls", "数据", PageRespVO.class,
                        BeanUtils.toBean(list, PageRespVO.class));
    }

}